package com.hzsd.zengbanyi.biaodan_text.Actrivity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.hzsd.zengbanyi.biaodan_text.BaseActivity;
import com.hzsd.zengbanyi.biaodan_text.MyAdapter.ImageGridAdapter;
import com.hzsd.zengbanyi.biaodan_text.R;
import com.hzsd.zengbanyi.biaodan_text.bean.AlbumHelper;
import com.hzsd.zengbanyi.biaodan_text.bean.ImageItem;
import com.hzsd.zengbanyi.biaodan_text.bean.PictureAdapter;
import com.hzsd.zengbanyi.biaodan_text.bean.WorkDAO;
import com.hzsd.zengbanyi.biaodan_text.utils.FileUtil2;
import com.hzsd.zengbanyi.biaodan_text.weight.Bimp;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;



/**
 * 添加图片页面
 * @author Administrator
 *
 */
public class PicAndVideo2Activity extends BaseActivity {
	public static final String EXTRA_IMAGE_LIST = "imagelist";
	WorkDAO workDAO;
	List<ImageItem> dataList;
	GridView gridView;
	ImageGridAdapter adapter;
	AlbumHelper helper;
	Button bt;
	String taskSceneId;
	//============
	PictureAdapter adapter2;
	String saveDir = Environment.getExternalStorageDirectory()  
            + "/hjjc/imgs/";
	//============
	Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case 0:
				Toast.makeText(PicAndVideo2Activity.this, "最多选择9张图片", Toast.LENGTH_SHORT).show();
				break;
			case 1:
				Toast.makeText(PicAndVideo2Activity.this, "已经有相同的图片", Toast.LENGTH_SHORT).show();
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.activity_pic_and_video2);
		helper = AlbumHelper.getHelper();
		helper.init(getApplicationContext());
		Bimp.bmp=new ArrayList<Bitmap>();
		dataList = (List<ImageItem>) getIntent().getSerializableExtra(
				EXTRA_IMAGE_LIST);

		initView();
		workDAO=new WorkDAO(this);
		SharedPreferences share = getSharedPreferences("user", MODE_PRIVATE);   
		taskSceneId=share.getString("taskSceneId","");
		bt = (Button) findViewById(R.id.bt);
		bt.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				File file = new File(saveDir);
				if(!file.exists()){
					file.mkdirs();
				}
				ArrayList<String> list = new ArrayList<String>();
				Collection<String> c = adapter2.map.values();
				Iterator<String> it = c.iterator();
	
				for (; it.hasNext();) {
					list.add(it.next());
				}
				
				if (Bimp.act_bool) {
					setResult(Activity.RESULT_OK);
					Bimp.act_bool = false;
				}
				
				for (int i = 0; i < list.size(); i++) {
					if (Bimp.bmp.size() < 9) {	
						try {
							Bitmap bm=Bimp.revitionImageSize(list.get(i));
							Bimp.bmp.add(bm);
							
						} catch (IOException e) {
							e.printStackTrace();
						}
					}

				}
				try {
					for (int i = 0; i < list.size(); i++) {		
						workDAO.addMediaPath(list.get(i), taskSceneId,1);
					}
					
				} catch (Exception e) {
					e.printStackTrace();
					}
//				Bundle bundle = new Bundle();
				Intent intent = new Intent(PicAndVideo2Activity.this,MedioActivity.class);
////				bundle.putStringArrayList("path", list);
////				intent.putExtras(bundle);
//				
				startActivity(intent);
				finish();
			}

		});
	}
	private void initView() {
//		rlReturn=(RelativeLayout)findViewById(R.id.rl_return);

		gridView = (GridView) findViewById(R.id.gridview);
		gridView.setSelector(new ColorDrawable(Color.TRANSPARENT));
		SharedPreferences share = getSharedPreferences("user", MODE_PRIVATE);   
		taskSceneId=share.getString("taskSceneId","");
		/*adapter = new ImageGridAdapter(PicAndVideo2Activity.this, dataList,
				mHandler,taskSceneId);*/
		String[] titles = null;
		try {
			titles = FileUtil2.getImageNames(saveDir);
		} catch (Exception e) {
			e.printStackTrace();
			return;
		}
		
        String[] imagePaths = new String[titles.length];  
        for (int i = 0; i < titles.length; i++) { 
            imagePaths[i]=saveDir+titles[i];
        } 
        adapter2 = new PictureAdapter(titles, imagePaths, PicAndVideo2Activity.this,mHandler,taskSceneId);
		gridView.setAdapter(adapter2);
		adapter2.setTextCallback(new PictureAdapter.TextCallback() {
			public void onListen(int count) {
				bt.setText("完成" + "(" + count + "/9)");
			}
		});

		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				adapter2.notifyDataSetChanged();
			}

		});

	}
}
